﻿Public Class Login

    ' TODO: Insert code to perform custom authentication using the provided username and password 
    ' (See http://go.microsoft.com/fwlink/?LinkId=35339).  
    ' The custom principal can then be attached to the current thread's principal as follows: 
    '     My.User.CurrentPrincipal = CustomPrincipal
    ' where CustomPrincipal is the IPrincipal implementation used to perform authentication. 
    ' Subsequently, My.User will return identity information encapsulated in the CustomPrincipal object
    ' such as the username, display name, etc.
    Dim ctx As New QLDBDataContext

    Function CheckUser(ByVal us As String, ByVal pas As String) As Boolean
        Dim v = (From q In ctx.users Where q.UserName = us And q.Pass = pas).Count
        If v = 0 Then
            Return False
        Else
            Return True
        End If
    End Function

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        Cursor = Cursors.WaitCursor
        If Trim(UsernameTextBox.Text.ToString()) = "" Then
            lbpa.Text = "UserName không được để trông:"
            lbpa.Visible = True
            Return
        Else
            lbpa.Visible = False
        End If
        If Trim(PasswordTextBox.Text.ToString()) = "" Then
            lbpa.Text = "Pass word không được để trống"
            lbpa.Visible = True
            Return
        Else
            lbpa.Visible = False
        End If
        If CheckUser(UsernameTextBox.Text, PasswordTextBox.Text) = True Then
            Dim f As New Form1 With {.UserName = UsernameTextBox.Text}
            Me.Hide()
            Cursor = Cursors.Default
            f.Show()

        Else
            lbpa.Text = "UserName Hoặc PassWord Sai:"
            lbpa.Visible = True
            Cursor = Cursors.Default
        End If
    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Me.Close()
    End Sub

End Class
